var verifyCode = new GVerify({
  id: "yzm",
  canvasId: "verifyCanvas", //canvas的ID
  type: "blend", //图形验证码默认类型blend:数字字母混合类型、number:纯数字、letter:纯字母
});
$(window).on('inited',function(){
  $('#loginform').validate({
    rules: {
      name: {
        required:true,
        minlength:6
      },
      pwd: {
        required:true,
        minlength:6
      },
    },
    submitHandler: function(form) {
      var yzm_test = verifyCode.validate(document.getElementById("yzm_input").value);
      if(yzm_test) {
        // alert("验证正确");
        // form.submit();
        $(form).on('submit',function(e){e.preventDefault()})
        var formObj=$(form).serializeObject()
        // formData.password=md5(formData.password)
        var formData={
          query:{"$or":[{name:formObj.name},{email:formObj.name}]},
          data:formObj
        }
        $.get('/user/login',formData,function(res){
          console.log(res)
          if(res.code==200){
            alert('登录成功')
            __.del('user')
            __.data('user',res.data[0])
            console.log(__.data('user'))
            location.href='/';
          }else{
            $('#yzm canvas').trigger('click')
            alert(res.msg)
          }
        })
      } else {
        // alert("验证码错误");
        $('#yzm_error').remove()
        $('#yzm_group').after('<label id="yzm_error" class="error">验证码错误</label>')
      }
    }
  })
  $('#reg-form').on('submit',function(e){e.preventDefault()});
  $('#reg-form').validate({
    rules: {
      name: {
        required:true,
        minlength:6
      },
      pwd: {
        required:true,
        minlength:6
      },
      email:{
        email:true,
        required:true,
      },
    },
    submitHandler:function(form){
      var reqData=$(form).serializeObject()      
      console.log(reqData)
      if($(form).find('#cpwd').val()!=reqData.pwd){
        $('#cpwd_error').remove()
        $('#cpwd-grpup').after('<label id="cpwd_error" class="error">两次密码不一致</label>');
        return;
      }
      $.post('/user/add',{query:reqData},function(res){
        console.log(res)
        if(res.code==200){
          alert('注册成功，请登录');
          location.reload();
        }
      })
    }
  })
})